#include <IconTheme>
#include <AmeDirs>

#include <QtDebug>
#include <QFile>
#include <QDir>
#include <sys/stat.h>


AmeIconThemeDir::AmeIconThemeDir(const QString &rootDir, const QString &dir, QSettings &config)
{
    QString path = rootDir + dir + "/";
    if (QDir(path).exists()) {

        //qDebug() << "ICON THEME DIR LOOK AT " << path;
        config.beginGroup(dir);

        mDir = path;
        mSize = config.value("size", 0).toInt();

        QString c = config.value("Context", "any").toString();
        c = c.toLower();
        if (c == "any")
            mCtx = AmeIconThemeDir::Any;
        else if (c == "actions")
            mCtx = AmeIconThemeDir::Action;
        else if ((c == "aplications") || (c == "apps"))
            mCtx = AmeIconThemeDir::Application;
        else if (c == "devices")
            mCtx = AmeIconThemeDir::Device;
        else if (c == "places")
            mCtx = AmeIconThemeDir::Place;
        else if (c == "mimetypes")
            mCtx = AmeIconThemeDir::MimeType;
        else if (c == "emblems")
            mCtx = AmeIconThemeDir::Emblem;
        else if (c == "animations")
            mCtx = AmeIconThemeDir::Animation;
        else if (c == "categories")
            mCtx = AmeIconThemeDir::Category;
        else if (c == "emotes")
            mCtx = AmeIconThemeDir::Animation;

        else
            mCtx = AmeIconThemeDir::Any;

        config.endGroup();

        if (mSize == 0)
            mValid = false;
        else
            mValid = true;
    } else
        mValid = false;
}

AmeIconThemeDir::~AmeIconThemeDir()
{
}

bool AmeIconThemeDir::isValid() const
{
	return mValid;
}

QString AmeIconThemeDir::dir() const
{
	return mDir;
}

int AmeIconThemeDir::size() const
{
        return mSize;
}

int AmeIconThemeDir::context() const
{
    return mCtx;
}

QStringList AmeIconThemeDir::icons() const
{
    // TODO: FINISH THIS
	return QStringList();
}

QString AmeIconThemeDir::iconPath(const QString &name) const 
{
    if (!mValid)
        return QString();


    const char *exts[] = { ".png", ".svg", ".svgz", ".svg.gz", ".xpm" };
    const int extCount = sizeof(exts) / sizeof(char *);

    for (int k=0; k < extCount; k++) {

        QString file = mDir + name + QLatin1String(exts[k]);

        qDebug() << "WILL TRY " << file;

        if (access(QFile::encodeName(file), R_OK) == 0) {
            qDebug() << "ICON PATH = " << file;
            return file;
        }
    }
    qDebug() << "NO ICON FOUND";
	return QString();
}


AmeIconTheme::AmeIconTheme(const QString &dir, QObject *parent)
    : QObject(parent),
      themeDir(dir),
      themeValid(false)
{
    setTheme("default");
}

AmeIconTheme::~AmeIconTheme()
{
}

QString AmeIconTheme::name() const
{
	return themeName;
}

QString AmeIconTheme::comment() const
{
        return themeComment;
}

QString AmeIconTheme::dir() const
{
	return themeDir;
}

bool AmeIconTheme::setTheme(const QString &theme)
{
    mDirs.clear();
    mSizes.clear();

	// 1. check if file index.theme or index.desktop exists
	QString fullPath = themeDir + theme + "/";
    QString fileName, themeFile(fullPath + "index.theme") , desktopFile(fullPath + "index.desktop");
	if (QFile::exists(themeFile)) 
		fileName = themeFile;
    else if (QFile::exists(desktopFile))
        fileName = desktopFile;
    else {
        //qDebug() << "THEME FILE NOT FOUND AT " << fullPath;
        themeValid = false;
        return false;
    }

    //qDebug() << "THEME FILE FOUND AT " << fullPath;

	themeName = theme;
	// 2. parse theme config file
    QSettings config(fileName, QSettings::IniFormat);

    config.beginGroup(QLatin1String("Icon Theme"));
    QStringList dirs = config.value(QLatin1String("Directories")).toStringList();
    themeComment = config.value(QLatin1String("Comment")).toStringList().join(QString(' '));
    qDebug() << "Comment = " << themeComment;
    config.endGroup();

    QStringList::ConstIterator it;
    for (it = dirs.begin(); it != dirs.end(); ++it) {
        AmeIconThemeDir *d = new AmeIconThemeDir(fullPath, *it, config);
        if (d->isValid()) {
            mDirs.insert(d->size(), d);
        }
    }

    mSizes = mDirs.uniqueKeys();
    if (mSizes.count() > 0 && mDirs.count() > 0) {
        themeValid = true;
    } else {
        themeValid = false;
    }

    qDebug() << "THEME VALID=" << themeValid;
    return themeValid;
}

QPixmap AmeIconTheme::loadIcon(const QString &name, int ctx, int size) const
{
    if (!themeValid)
        return NULL;

    QString path;
    QList <AmeIconThemeDir *> dirs;
    int prefSize;

    if (mDirs.contains(size)) {
        // exact size match found
        dirs = mDirs.values(size);
    } else {
        prefSize = mSizes.last();
        QList<int>::ConstIterator it = mSizes.constEnd();
        // search the best match, preferably greater size dir
        while (it != mSizes.constBegin()) {
            if (*it < prefSize)
                prefSize = *it;
            if (*it > prefSize)
                break;
            --it;
        }
        dirs = mDirs.values(prefSize);
    }
    // ok. now have a list of AmeIconThemeDir's with the best icon size, lets find the desired icon

    for (int i=0; i <= dirs.count(); i++) {
        if (ctx != AmeIconThemeDir::Any && ctx != dirs.at(i)->context())
            continue;
        path = dirs.at(i)->iconPath(name);
        qDebug() << "LOOK FOR ICON IN " << path;

        if (path.isEmpty())
            continue;

        if (QFile::exists(path)) {
            qDebug() << "ICON FOUND. PATH=" << path;
            return QPixmap(path);
        }
    }

    return QPixmap();
}

void AmeIconTheme::paint(QPainter *paint, const QRect &rect, const QString &name, QIcon::Mode mode, QIcon::State state)
{
}

AME_GLOBAL_STATIC_WITH_ARGS(AmeIconTheme, globalIconTheme, 
	(AmeDirs::global()->stdDir(AmeDirs::IconTheme)))




